package com.ruoyi.web.controller.StoreRealMoney;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.StoreRealMoney.domain.StoreRealmoney;
import com.ruoyi.StoreRealMoney.service.IStoreRealmoneyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;

/**
 * 商家提现 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-08-01
 */
@Controller
@RequestMapping("/StoreRealMoney/storeRealmoney")
public class StoreRealmoneyController extends BaseController
{
    private String prefix = "StoreRealMoney/storeRealmoney";
	
	@Autowired
	private IStoreRealmoneyService storeRealmoneyService;
	
	@Autowired
	private ISysUserService userService;
	
	@RequiresPermissions("StoreRealMoney:storeRealmoney:view")
	@GetMapping()
	public String storeRealmoney()
	{
	    return prefix + "/storeRealmoney";
	}
	
	/**
	 * 查询商家提现列表
	 */
	@RequiresPermissions("StoreRealMoney:storeRealmoney:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(StoreRealmoney storeRealmoney)
	{
		SysUser currentUser=ShiroUtils.getSysUser();
		String storeName=currentUser.getUserName();
		if(!"系统管理员".equals(storeName)){
			storeRealmoney.setStoreName(storeName);
		}
		storeRealmoney.setValid(2);
		startPage();
        List<StoreRealmoney> list = storeRealmoneyService.selectStoreRealmoneyList(storeRealmoney);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商家提现列表
	 */
	@RequiresPermissions("StoreRealMoney:storeRealmoney:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StoreRealmoney storeRealmoney)
    {
    	List<StoreRealmoney> list = storeRealmoneyService.selectStoreRealmoneyList(storeRealmoney);
        ExcelUtil<StoreRealmoney> util = new ExcelUtil<StoreRealmoney>(StoreRealmoney.class);
        return util.exportExcel(list, "storeRealmoney");
    }
	
	/**
	 * 新增商家提现
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商家提现
	 */
	@RequiresPermissions("StoreRealMoney:storeRealmoney:add")
	@Log(title = "商家提现", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(StoreRealmoney storeRealmoney)
	{		
		SysUser currentUser=ShiroUtils.getSysUser();
		Long id=currentUser.getUserId();
		Double getMoney=storeRealmoney.getMoney();
		Double accountMoney=currentUser.getAccountMoney();
		if(getMoney>accountMoney){
			return AjaxResult.error("提现金额超出余额，请填写正确数值");
		}
		Double afterMoney=accountMoney-getMoney;
		SysUser persist=userService.selectUserById(id);
		persist.setAccountMoney(afterMoney);
		userService.updateUserInfo(persist);
		String storeName=currentUser.getUserName();
		storeRealmoney.setAccount(accountMoney);
		storeRealmoney.setCreateDate(DateTools.getYMDhms());
		storeRealmoney.setState(0);
		storeRealmoney.setValid(1);
		storeRealmoney.setStoreId(id.intValue());
		storeRealmoney.setStoreName(storeName);
		return toAjax(storeRealmoneyService.insertStoreRealmoney(storeRealmoney));
	}

	/**
	 * 修改商家提现
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		StoreRealmoney storeRealmoney = storeRealmoneyService.selectStoreRealmoneyById(id);
		mmap.put("storeRealmoney", storeRealmoney);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商家提现
	 */
	@RequiresPermissions("StoreRealMoney:storeRealmoney:edit")
	@Log(title = "商家提现", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(StoreRealmoney storeRealmoney)
	{		
		return toAjax(storeRealmoneyService.updateStoreRealmoney(storeRealmoney));
	}
	
	/**
	 * 删除商家提现
	 */
	@RequiresPermissions("StoreRealMoney:storeRealmoney:remove")
	@Log(title = "商家提现", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(storeRealmoneyService.deleteStoreRealmoneyByIds(ids));
	}
	
}
